package com.yeqian.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yeqian.entity.PageResult;
import com.yeqian.entity.QueryPageBean;
import com.yeqian.pojo.Tag;
import com.yeqian.service.BaseService;
import com.yeqian.service.TagService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Author : yeqian
 * @Date : 2020/12/31 23:18
 * 标签服务实现类
 */
@Transactional
@Service
public class TagServiceImpl extends BaseService implements TagService {

    /**
     * 查询标签占比
     *
     * @param
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @author yeqian
     * @date 2021/1/16 18:10
     */
    @Override
    public List<Map<String, Object>> getReport() {
        return tagMapper.getReport();
    }

    /**
     * 查询标签总数量
     *
     * @param
     * @return java.lang.Integer
     * @author yeqian
     * @date 2021/1/16 11:36
     */
    @Override
    public Integer getTagCount() {
        return tagMapper.getTagCount();
    }

    /**
     * 获取标签的封面图片
     *
     * @param
     * @return java.util.List<java.lang.String>
     * @author yeqian
     * @date 2021/1/10 14:57
     */
    @Override
    public List<String> getUrl() {
        List<String> urls = tagMapper.getUrl();
        List<String> urlList = new LinkedList<>();
        //截取成我们可以删除的url
        for (String url : urls) {
            String[] split = url.split("com/");
            urlList.add(split[1]);
        }
        return urlList;
    }

    /**
     * 查询所有标签
     *
     * @param
     * @return java.util.List<java.lang.String>
     * @author yeqian
     * @date 2021/1/10 14:57
     */
    @Override
    public List<String> listAll() {
        return tagMapper.selectAllName();
    }

    /**
     * 删除标签
     *
     * @param tagId
     * @return void
     * @author yeqian
     * @date 2021/1/1 17:03
     */
    @Override
    public void deleteTag(Integer tagId) {

        //先删除关系
        tagMapper.deleteTagAndArticle(tagId);

        //在删除标签
        tagMapper.deleteByPrimaryKey(tagId);
    }

    /**
     * 编辑标签
     *
     * @param tag
     * @return void
     * @author yeqian
     * @date 2021/1/1 16:47
     */
    @Override
    public void updateTag(Tag tag) {
        tagMapper.updateByPrimaryKey(tag);
    }

    /**
     * 根据主键查询标签
     *
     * @param tagId
     * @return com.yeqian.pojo.Tag
     * @author yeqian
     * @date 2021/1/1 15:31
     */
    @Override
    public Tag getTag(Integer tagId) {
        return tagMapper.getTag(tagId);
    }

    /**
     * 新增标签
     *
     * @param tag
     * @return void
     * @author yeqian
     * @date 2021/1/1 11:16
     */
    @Override
    public void saveTag(Tag tag) {
        tagMapper.saveTag(tag);
    }

    /**
     * 分页查询标签
     *
     * @param queryPageBean
     * @return com.yeqian.entity.PageResult
     * @author yeqian
     * @date 2020/12/31 23:18
     */
    @Override
    public PageResult listQueryPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage, pageSize);
        Page<Tag> page = tagMapper.listQueryPage(queryString);
        List<Tag> result = page.getResult();
        for (Tag tag : result) {
            setArticleCount(tag); //设置标签中所对应的文章数量
        }
        long total = page.getTotal();
        return new PageResult(total, page);
    }


    /**
     * 公共代码抽取
     * 设置标签中所对应的文章数量
     *
     * @param
     * @return void
     * @author yeqian
     * @date 2020/12/31 23:45
     */
    public void setArticleCount(Tag tag) {
        Integer tagId = tag.getId();
        Integer articleTag = tagMapper.getArticleTagCountById(tagId);
        tag.setArticleCount(articleTag);
    }
}
